package ee.reliability.tool.util;

import java.util.Calendar;
import java.util.Date;

@SuppressWarnings("serial")
public class BigFriday extends Date {
	@SuppressWarnings("deprecation")
	@Override
	public boolean equals(Object obj) {
		return obj instanceof Date &&
				bigFriday(((Date) obj).getYear() + 1900).equals(obj);
	}

	private static final int M = 24,  N = 5;
	private Date bigFriday(int year) {
		int a = year % 19;
		int b = year % 4;
		int c = year % 7;
		int d =  (19 * a + M) % 30;
		int e = (2 * b + 4 * c + 6 * d + N) % 7;
		int f = 22 + d + e;
		if ((f == 57) || (f == 56 && e == 6 && a > 10))
			f = f - 7;
		f -= 2;
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(year, Calendar.MARCH, f);
		return cal.getTime();
	}
}
